home *** CD-ROM | disk | FTP | other *** search
- Path: news.sprintlink.net!datalytics!news
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: ode to c++
- Date: 4 Jan 1996 19:00:26 GMT
- Organization: Datalytics, Inc
- Message-ID: <4ch84a$ldg@gold.datalytics.com>
- References: <4bq620$p1t@news.iconn.net>
- NNTP-Posting-Host: pc071.datalytics.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
-
- thecrow@iconn.net (The Crow) wrote:
- >About 2 months ago I got very into Pascal (because it was the only thing on the
- >network at my highschool) I wrote some pretty cool stuff with it. I also
- >messed around with VB and found it to be FAR too slow at number crunching.
- >Then I finally sat down with C++ these last two days. I rewrote a simple
- >Pascal program in C++ that calculates Pythagorean triples, here is the C++
- >version
- >
- >// The amazing Pythagorean Triple Generator
- >#include <stdio.h>
- >
- >main()
- >{
- >const deep = 13200;
- >long a = 1,b = 1,c = 1;
- >
- > for(c = 1; c<= deep; c++)
- > {
- > for(b = c/2; b <= c; b++)
- > {
- > for(a = (c-b)+1; a <= b; a++)
- > {
- > if (a*a + b*b > c * c) break;
- > if (a*a + b*b == c * c)
- > printf("%ld %ld %ld \n",a,b,c);
- > } // for a
- > } // for b
- > } // for c
- >} // main
- >
- >
- >The Pascl version took 20 seconds for C to get to 500, C++ took 6 seconds! It
- >blew my mind!, I'm never going back! long live C!
- >
- >
- >--
- >The Crow - thecrow@iconn.net
- >"It can't rain all the time"
- >-Kryptology
- >
-
- Now you know why so many people prefer C and C++. They aren't
- the easiest languages to learn, but they are screamers. Now,
- two points of order. First, you wrote a C program. Without
- resorting to OOP, I'll show you how to take advantage of some
- better things you can do with C++. Second, you are falling
- into some stylistic traps that I would like to correct.
-
- The code below illustrates both points:
-
- #include <assert.h> // assert
- #include <iostream.h> // cout
- #include <math.h> // sqrt
- #include <stddef.h> // stddef.h
-
- typedef unsigned long index;
-
- int main(void)
- {
- const int DEEP = 13200;
- const index maxIndex = sqrt(ULONG_MAX);
- for (index c = 1; c <= DEEP; c++)
- {
- for (index b = c / 2; b <= c; b++)
- {
- for (index a = (c - b) + 1; a <= b; a++)
- {
- assert(a < maxIndex);
- index a2 = a * a;
- assert(b < maxIndex);
- index b2 = b * b;
- index sumOfSquares = a2 + b2;
- assert(c < maxIndex);
- index c2 = c * c;
- if (sumOfSquares > c2)
- {
- break;
- }
- if (sumOfSquares == c2)
- {
- cout << a << ' ' << b << ' ' << c << ' '
- << endl;
- }
- }
- }
- }
- return 0;
- }
-
- Note that I change the case of "deep." Making it uppercase
- makes its being const stylistically obvious. Note also that I
- declared and initialized the for loop indexes in the for loop
- statements. At the same time, I changed their type to size_t.
- This is a standard typedef to the native type for the current
- environment. It is usually a good choice for loop indices.
- (On the other hand, for some environments, it could prove too
- small for your purposes.)
-
- Note that you must be certain of the capacity of the type you
- use for the loop indexes. You used a signed type for an
- unsigned value, so you immediately lost half the capacity to
- the sign bit. Furthermore, squaring the indexes could exceed
- the capacity of the type you selected. And, of course, adding
- the square of a to the square of b could exceed the capacity.
- As originally written, your program would simply provide
- incorrect answers because the values in the conditionals would
- wrap to small numbers.
-
- I used the assert macro to report a value out of range and
- terminate the program. The assert macro conditional compiles
- to a NOP if you #define NDEBUG.
-
- I also added whitespace around all binary operators. This
- makes the code a little more readable. I also removed the
- duplicate calculations in the inner for loop by creating a
- couple of temporaries. (Do note the identifiers I chose a
- reasonably indicative of what they represent.)
-
- Finally, I wrote the if statements in the block style for
- consistency. Many old C programmers still use the (perfectly
- legal, but harder to read and more error prone) style you
- used. However, as soon as the conditional requires a second
- statement, you must use the block style. Without the block
- style you can get confused as to what belongs to the
- conditional and what doesn't. (This has been the subject of
- PC-Lint advertisements for years.)
-
- My stylistic comments are subjective, to be sure, but well
- conceived and time-tested.
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc.
- (513)226-7700
- stew@datalytics.com
-
-
-